package com.bustle.web.mbean.session;

import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import com.bustle.order.AccountManager;
import com.bustle.order.OrderManager;
import com.bustle.order.data.Lineitem;
import com.bustle.order.data.Order;
import com.bustle.order.data.Order.OrderStatus;
import com.bustle.product.ProductDtoManager;
import com.bustle.product.dto.product.DtoToProductMapConverter;
import com.bustle.product.dto.product.ProductDto;
import com.bustle.web.mbean.request.IdentificationMBean;
import com.bustle.web.mbean.request.PaymentMBean;

@ManagedBean(name = "cartMBean")
@SessionScoped
public class CartMBean
{
  public String confirm()
  {
    Integer userId = userMBean.getId();
    if (userId != null)
    {
      Order order = new Order();
      order.setCreationDate(new Date());
      order.setAccount(accountManager.getByUser(userId));
      order.setOrderStatus(OrderStatus.WAITING);
      for (LineitemBean lineitemBean : lineitemBeans)
      {
        Lineitem lineitem = new Lineitem();
        lineitem.setProduct(dtoToProductConverter.convert(lineitemBean.getProduct()));
        lineitem.setQuantity(lineitemBean.getQuantity());
        order.getLineItems().add(lineitem);
      }
      orderManager.createOrder(order);

      lineitemBeans.clear();
      orderId = order.getIdOrder();
      return PaymentMBean.SCREEN;
    }
    return IdentificationMBean.SCREEN;
  }

  public List<LineitemBean> getLineitemBeans()
  {
    return lineitemBeans;
  }

  public double getTotalPrice()
  {
    double total = 0;
    for (LineitemBean lineitemBean : lineitemBeans)
    {
      total += lineitemBean.getTotal();
    }
    return total;
  }

  public int getOrderId()
  {
    return orderId;
  }

  public void setUserMBean(UserMBean aUserMBean)
  {
    userMBean = aUserMBean;
  }

  public void reset()
  {
    lineitemBeans.clear();
    orderId = 0;
  }

  protected List<LineitemBean> lineitemBeans = new Vector<>();
  protected int orderId;
  @EJB
  protected OrderManager orderManager;
  @EJB
  protected ProductDtoManager productManager;
  @EJB
  protected AccountManager accountManager;
  @EJB
  protected DtoToProductMapConverter dtoToProductConverter;
  @ManagedProperty("#{userMBean}")
  protected UserMBean userMBean;


  public class LineitemBean
  {
    public String add()
    {
      // TODO use a selected quantity and update from it
      quantity++;
      return null;
    }

    public String remove()
    {
      // As above
      quantity--;
      return null;
    }

    public ProductDto getProduct()
    {
      return productManager.getById(idProduct);
    }

    public int getIdProduct()
    {
      return idProduct;
    }

    public void setIdProduct(int anIdProduct)
    {
      idProduct = anIdProduct;
    }

    public int getQuantity()
    {
      return quantity;
    }

    public void setQuantity(int aQuantity)
    {
      quantity = aQuantity;
    }

    public double getTotal()
    {
      return getProduct().getPrice() * quantity;
    }

    protected int idProduct;
    protected int quantity;
  }
}
